class CPU:

    def __init__(self):
        self.programCounter = 0x100
        self.stackPointer = 0xFFFE
    
          
    def executeOpcode(self, opcode):

        return 1
    
    def executeNextOpcode(self, mmu):
       
        cycles = 0
        opcode = 0
        print mmu.rom[self.programCounter]
        opcode = mmu.readMemory(self.programCounter)
        self.programCounter += 1
        cycles = self.executeOpcode(opcode)
        return cycles


class MMU:

    def __init__(self): 
        self.cartridgeMemory = []
        self.mapperNumber = 0
        self.title = ""
        self.romSize = 0
        self.banking = 0
        
        self.rom = [0 for x in range(0x0, 0x10000)]
        
        
        self.MBC1 = False
        self.MBC2 = False
        self.currentROMBank = 1
        
        self.ramBanks = [0 for x in range(0x0, 0x8000)]
        self.currentRAMBank = 0
        
    def loadRom(self, romPath):
        try:
            romFile = open(romPath, 'rb')
        except ValueError:
            print "File I/O Error. Check the path of the rom file."
			

        self.cartridgeMemory = romFile.read(0x200000)
        self.mapperNumber = self.cartridgeMemory[0x147]
        self.title = self.cartridgeMemory[0x134:0x143]
        self.banking = self.cartridgeMemory[0x147]

        if self.banking == 1:
            MBC1 = True
        elif self.banking == 2:
            MBC1 = True
        elif self.banking == 3:
            MBC1 = True
        elif self.banking == 5:
            MBC2 = True
        elif self.banking == 6:
            MBC2 = True     
        
        print "Game title = ", self.title
        print "Mapper number =", ord(self.mapperNumber)
        
    
    def romInit(self):
    
        #Preset values from pandocs
        self.rom[0xFF05] = 0x00  
        self.rom[0xFF06] = 0x00  
        self.rom[0xFF07] = 0x00  
        self.rom[0xFF10] = 0x80  
        self.rom[0xFF11] = 0xBF  
        self.rom[0xFF12] = 0xF3  
        self.rom[0xFF14] = 0xBF  
        self.rom[0xFF16] = 0x3F  
        self.rom[0xFF17] = 0x00  
        self.rom[0xFF19] = 0xBF  
        self.rom[0xFF1A] = 0x7F  
        self.rom[0xFF1B] = 0xFF  
        self.rom[0xFF1C] = 0x9F  
        self.rom[0xFF1E] = 0xBF  
        self.rom[0xFF20] = 0xFF  
        self.rom[0xFF21] = 0x00  
        self.rom[0xFF22] = 0x00  
        self.rom[0xFF23] = 0xBF  
        self.rom[0xFF24] = 0x77  
        self.rom[0xFF25] = 0xF3 
        self.rom[0xFF26] = 0xF1	  
        self.rom[0xFF40] = 0x91  
        self.rom[0xFF40] = 0x91  
        self.rom[0xFF42] = 0x00  
        self.rom[0xFF43] = 0x00  
        self.rom[0xFF45] = 0x00  
        self.rom[0xFF47] = 0xFC  
        self.rom[0xFF48] = 0xFF  
        self.rom[0xFF49] = 0xFF  
        self.rom[0xFF4A] = 0x00  
        self.rom[0xFF4B] = 0x00  
        self.rom[0xFFFF] = 0x00
        
    
    def readMemory(self, address):
        
        if ((address >= 0x4000) & (address <= 0x7FFF)):
        
            newAddress = address - 0x4000 
            return self.cartridgeMemory[newAddress + (self.currentROMBank*0x4000)]
            
        elif ((address >= 0xA000) & (address <= 0xBFFF)):
            newAddress = address - 0xA000
            return self.ramBanks[newAddress + (self.currentRAMBank*0x2000)]
        
        else:
            return self.rom[address]
              
    def writeMemory(self, address):

        if address < 0x8000:
            pass
       
        elif ((address >= 0xE000) & (address < 0xFE00)): 
            self.rom[address] = data
            self.writeMemory(address-0x2000, data)
        
        elif ((address >= 0xFEA0) & (address < 0xFEFF)):
            pass
        
        else:
            self.rom[address] = data ;
        
        
    
class Pixel:
    def __init__(self):
        color = (0, 0, 0, 0)
    
class Screen:
    
    def __init__(self):
        screen = []
        width = 160
        height = 144
       
        row = []
        
        for x in range(0, width):
            row.append(Pixel())
        
        for y in range(0, height):
            screen.append(row)
            